Dictionary

A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values.Each key is separated from its value by a colon (:) while each element is separated by commas.

Creating a Dictionary
In python a Dictionary can be created by placing sequence of elements within curly braces and separated by 'comma'. Dictionary holds a pair of values, one being key and other corresponding values. Values in a dictionary can be of any datatype and can be duplicated, whereas keys can’t be repeated and must be immutable.
#Creating and print a dictionary:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(car)
{'year': 1964, 'model': 'Mustang', 'brand': 'Ford'}
# with Integer Keys
Dict = {1: 'Python', 2: 'Scala', 3: 'Java'}
print("\n Dictionary with the use of Integer Keys:")
print(Dict)
Dictionary with the use of Integer Keys:
{1: 'Python', 2: 'Scala', 3: 'Java'}
# with Mixed keys
Dict = {'Language': 'Python', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
Dictionary with the use of Mixed Keys:
{1: [1, 2, 3, 4], 'Language': 'Python'}

# Creating an empty Dictionary
Dict = {}
print("Empty Dictionary: ")
print(Dict)
Empty Dictionary:
{}
# with dict() method
Dict = dict({1: 'Python', 2: 'Scala', 3: 'Java'} )
print("\n Dictionary with the use of dict(): ")
print(Dict)
# with each item as a Pair
Dict = dict([(1, 'Python'), (2, 'Scala')])
print("\n Dictionary with each item as a pair: ")
print(Dict)
Accessing Dictionary Items
You can access the items of a dictionary by referring to its key name, inside square brackets:
# Accessing dictionary Items:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(car["year"])
1964

There is also a method called get() that will give you the same result:
Get the value of the "model" key:
# Accessing dictionary Items:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.get("model")
print(x)
Mustang

Change Dictionary Value
You can change the value of a specific item by referring to its key name:
Change the "year" to 1990:
car ={
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(car)
car["year"]=1990
print(car)
Dictionary len
To determine how many items (key-value pairs) a dictionary has, use the len() method.

Print the number of items in the dictionary:
# Accessing dictionary Items:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(len(car))
3
# Creating an empty Dictionary 
Dict = {}
print("Empty Dictionary: ")
print(Dict)

# Adding elements one at a time
Dict[0] = 'Geeks'
Dict[2] = 'For'
Dict[3] = 1
print("\nDictionary after adding 3 elements: ")
print(Dict)

# Adding set of values
# to a single Key
Dict['Value_set'] = 2, 3, 4
print("\nDictionary after adding 3 elements: ")
print(Dict)

# Updating existing Key's Value
Dict[2] = 'Welcome'
print("\nUpdated key value: ")
print(Dict)

# Adding Nested Key value to Dictionary
Dict[5] = {'Nested': {'1': 'Life', '2': 'Geeks'}}
print("\nAdding a Nested Key: ")
print(Dict)

No comments:

Post a Comment